package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.enums.GiftEnum;
import com.prince.enums.SaleEnum;
import com.prince.model.SalesRecord;
import com.prince.page.Page;
import com.prince.util.PoiExcelExportUtil;
import com.prince.web.base.BaseController;
import com.prince.web.service.SalesRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class SalesRecordController extends BaseController {

    @Autowired
    private SalesRecordService salesRecordService;

    /**
     * 功能描述: 分页查询销售明细
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @AppLog
    @PostMapping("/findSalesRecordByPage")
    public ResponseEntity<Map<String,Object>> findSalesRecordByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = salesRecordService.findSalesRecordByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }
    /**
     * 功能描述: 通过id查询订单明细
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/14 15:52
     */
    @AppLog
    @PostMapping("/findOrderDetails")
    public ResponseEntity<Map<String,Object>> findOrderDetails(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = salesRecordService.findOrderDetails(params,page);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }

    /**
     * 功能描述: 导出销售明细
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/exportSalesRecord")
    public void exportSalesRecord(@RequestParam Map<String,Object> params, HttpServletResponse response){
        Map<String,Object> map = salesRecordService.findSalesRecordByPage(params,null);
        Object object = map.get("rows");
        if(null == object) {
            return;
        }
        List<SalesRecord> list = (List<SalesRecord>)object;
        list.forEach(x->{
            x.setRemark(SaleEnum.getName(x.getDirection()));
            x.setDescribed(GiftEnum.getName(x.getType()));
        });
        //导出
        Date date = new Date();
        PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String strDate = simpleDateFormat.format(date);
        String[] titleColumns = {  "orderId","name","supplier", "productUnit","specifications","remark","described","number","purchasePrice","price","creater","created"};
        String[] titleNames = {"订单号","商品名称","供应商","单位", "规格","状态", "商品类型","数量","零售价","进货价","操作人","时间"};
        //生成下载流
        poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "销售明细-" + strDate);

    }
}
